// 导航栏滚动效果
window.addEventListener('scroll', function() {
  const navbar = document.getElementById('navbar');
  if (window.scrollY > 20) {
    navbar.classList.add('scrolled');
  } else {
    navbar.classList.remove('scrolled');
  }
});

// 用户菜单切换
const userMenuBtn = document.getElementById('userMenuBtn');
const userMenu = document.getElementById('userMenu');

userMenuBtn.addEventListener('click', function() {
  userMenu.classList.toggle('hidden');
});

// 移动端菜单切换
const mobileMenuBtn = document.getElementById('mobileMenuBtn');
const mobileMenu = document.getElementById('mobileMenu');

mobileMenuBtn.addEventListener('click', function() {
  mobileMenu.classList.toggle('hidden');
});

// 点击页面其他地方关闭下拉菜单
document.addEventListener('click', function(event) {
  if (!userMenuBtn.contains(event.target) && !userMenu.contains(event.target)) {
    userMenu.classList.add('hidden');
  }
  
  if (!mobileMenuBtn.contains(event.target) && !mobileMenu.contains(event.target)) {
    mobileMenu.classList.add('hidden');
  }
});

// 评分功能
const ratingContainers = document.querySelectorAll('[data-rating]');

ratingContainers.forEach(container => {
  const stars = container.querySelectorAll('i');
  const ratingValue = container.getAttribute('data-rating');
  const name = container.getAttribute('data-name');
  const ratingDisplay = document.getElementById(`${name}Rating`);
  
  // 初始化星星状态
  stars.forEach((star, index) => {
    if (index < ratingValue) {
      star.classList.add('text-yellow-400');
      star.classList.remove('text-gray-300');
    } else {
      star.classList.add('text-gray-300');
      star.classList.remove('text-yellow-400');
    }
    
    // 添加点击事件
    star.addEventListener('click', () => {
      // 更新星星状态
      stars.forEach((s, i) => {
        if (i <= index) {
          s.classList.add('text-yellow-400');
          s.classList.remove('text-gray-300');
        } else {
          s.classList.add('text-gray-300');
          s.classList.remove('text-yellow-400');
        }
      });
      
      // 更新评分值
      container.setAttribute('data-rating', index + 1);
      ratingDisplay.textContent = `${index + 1}分`;
    });
  });
});

// 文字计数器
const commentsTextarea = document.getElementById('comments');
const charCount = document.getElementById('charCount');

commentsTextarea.addEventListener('input', function() {
  const count = this.value.length;
  charCount.textContent = count;
  
  // 限制最大字数
  if (count > 500) {
    this.value = this.value.substring(0, 500);
    charCount.textContent = 500;
  }
});

// 表单提交
const evaluationForm = document.getElementById('evaluationForm');
const successModal = document.getElementById('successModal');
const closeSuccessModalBtn = document.getElementById('closeSuccessModalBtn');

evaluationForm.addEventListener('submit', function(event) {
  event.preventDefault();
  
  // 表单验证
  const comments = document.getElementById('comments').value;
  if (comments.length < 20) {
    alert('请至少输入20个字符的评价内容');
    return;
  }
  
  // 这里可以添加表单提交逻辑
  console.log('提交评价表单');
  
  // 显示成功模态框
  successModal.classList.remove('hidden');
});

closeSuccessModalBtn.addEventListener('click', function() {
  successModal.classList.add('hidden');
  // 返回个人中心或刷新页面
  window.location.href = 'personal-center.html';
});

// 为所有卡片添加动画效果
document.addEventListener('DOMContentLoaded', function() {
  const cards = document.querySelectorAll('.transform');
  cards.forEach((card, index) => {
    setTimeout(() => {
      card.classList.add('animate-fade-in');
    }, 100 * index);
  });
});
    